<?php
	  require_once('conn.php');
	  header("content-type:text/html;charset=utf-8");
	  $sid = isset($_GET["sid"]) ? $_GET["sid"] : "";
	  $action = isset($_GET["act"]) ? $_GET["act"] : "";
	  if(empty($sid) || !is_numeric($sid)){
		  mysql_close($conn);
		  die("<script language='javascript' type='text/javascript'>alert('主题参数不能为空且只能是数字!');window.close();</script>");
	  }  
	  $query = mysql_query("select * from xh_subject where id=".$sid,$conn);
	  if(mysql_num_rows($query)<=0){
		  mysql_close($conn);
		  die("<script language='javascript' type='text/javascript'>alert('该主题不存在或被删除!');window.close();</script>");	  
	  }  
	  if($cfg->getallowview()==1){
		  if(empty($_SESSION["admin"])){
			  mysql_close($conn);
			  die("<script language='javascript' type='text/javascript'>alert('管理员设置为不允许查看,如果您是管理员,请先登录!');location.href='login.php';</script>");		  
		  }
	  }
	  
	  if($action=="vote"){
		  if($cfg->getuserinfo()==1){
			  $name=trim(htmlspecialchars($_POST["username"],ENT_QUOTES));
			  $sex =trim(htmlspecialchars($_POST["sex"],ENT_QUOTES));
			  $idcard=trim(htmlspecialchars($_POST["idcard"],ENT_QUOTES));
			  $tel=trim(htmlspecialchars($_POST["tel"],ENT_QUOTES));
			  $address=trim(htmlspecialchars($_POST["address"],ENT_QUOTES));
			  $email=trim(htmlspecialchars($_POST["email"],ENT_QUOTES));
			  $content=trim(htmlspecialchars($_POST["remarks"],ENT_QUOTES));
			  $ip=getip();
			  //服务器端判断名称身份证 邮箱不能为空
			  if(empty($name)){
				  mysql_close($conn);
				  die("<script language='javascript' type='text/javascript'>alert('用户名不能为空!');history.back(1);</script>");
			  }
			  if(empty($idcard)){
				  mysql_close($conn);
				  die("<script language='javascript' type='text/javascript'>alert('身份证号不能为空!');history.back(1);</script>");
			  }
			  if(empty($email))
			  {
				  mysql_close($conn);
				  die("<script language='javascript' type='text/javascript'>alert('邮箱不能为空!');history.back(1);</script>");
			  }		  		  
		  }
		  if($cfg->getrestrictip()==1){
			  
			  $ip=getip();
			  $cmd="select * from xh_ip where sid=".$sid." and ip='".$ip."' and DATE_ADD(vtime,INTERVAL ".$cfg->getrefusetime()." HOUR)>NOW()";
			  echo $cmd;			  
			  $query= mysql_query($cmd,$conn);
			  if(mysql_num_rows($query)>0){
				  mysql_free_result($query);
				  mysql_close($conn);
				  die("<script language='javascript' type='text/javascript'>alert('您已经投过票了!');history.back(1);</script>");
				  
			  }else{
				  $cmd="delete from xh_ip where sid=".$sid." and ip='".$ip."'";
				  $cmd="insert into xh_ip (sid,ip,vtime) values(".$sid.",'".$ip."','".date("Y-m-d H:i:s",time())."')";
				  mysql_query($cmd,$conn);
			  }
			  mysql_free_result($query);		  
		  }	  
		  $query=mysql_query("select * from xh_title where sid=".$sid,$conn);
		  $qids="";
		  $vote=false; //查看用户是否选择了投票项 如果没有选择项就退回上一个页面
		  while($row=mysql_fetch_array($query))
		  {
			  if(isset($_POST[$row["id"]]) && is_array($_POST[$row["id"]]) && count($_POST[$row["id"]])>0)
			  {
				  $qids .= implode(",",$_POST[$row["id"]]) + ","; //获取所有问题的id
				  $qidarr = $_POST[$row["id"]];
				  for($i=0;$i<count($qidarr);$i++)
				  {
					  $update=mysql_query("update xh_question set ps=ps+1 where id=".$qidarr[$i],$conn);
					  if($row["ms"]==1){
						  $update=mysql_query("update xh_title set vcount=vcount+1 where id=".$row["id"],$conn);
//						  echo "update xh_title set vcount=vcount+1 where id=".$row["id"];
					  }
				  }
				  if($row["ms"]==2){
					  $update=mysql_query("update xh_title set vcount=vcount+1 where id=".$row["id"],$conn);
				  }
				  $vote=true;
			  }
		  }
		  if(!$vote){
			  mysql_close($conn);
			  die("<script language='javascript' type='text/javascript'>alert('您没有选择投票项');history.back(1);</script>");
		  }
		  if($cfg->getuserinfo()==1){		  
			  mysql_query("insert into xh_userinfo (name,sex,idcard,tel,address,email,content,sid,qids,ip) values('$name','$sex','$idcard','$tel','$address','$email','$content','$sid','$qids','$ip')",$conn);		  
		  }
			  
	  }
?>
<html>

<head>
<?php 
  head();
?>
<title>查看投票结果</title>
<style>
.v_XHList{
background-color: #E1F0FF}
.v_XHList2{
background-color: #8EC7FF}
</style>
</head>

<body>

<div align="center">
	<?php	  
	  $query = mysql_query("select * from xh_title where sid=".$sid." order by id asc",$conn);
	  if(mysql_num_rows($query)<=0){
		  mysql_close($conn);
		  die("<script language='javascript' type='text/javascript'>alert('没有投票数据!');window.close();</script>");		  
	  }
	  while($row=mysql_fetch_array($query))
	  {
	?>
	<table width="40%" id="table1" cellspacing="1" cellpadding="4" bgcolor="#E1F0FF">
		<tr>
			<th colspan="4" bgcolor="#8EC7FF"><?php echo $row["title"] ?></th>
		</tr>
		<tr>
			<td colspan="4" align="center" bgcolor="#BFDFFF">总投票人数：<?php echo $row["vcount"];?>人</td>
		</tr>
		<?php
		  $total=0;
		  $qry = mysql_query("select sum(ps) as ps from xh_question where tid=".$row["id"],$conn);
		  if($trow=mysql_fetch_array($qry)){
			  $total=$trow["ps"];
		  }
		  mysql_free_result($qry);
		  $qry = mysql_query("select * from xh_question where tid=".$row["id"]." order by id asc",$conn);
	      $i=0;
	      while($qrow=mysql_fetch_array($qry))
		  {
			  $i++;
	    ?>
		<tr onMouseOver="this.className='v_XHList2';" onMouseOut="this.className='v_XHList';">
			<td align="center"><?php echo trim($qrow["question"]);?></td>
			<td width="10%" nowrap><b><?php echo $qrow["ps"];?></b>票</td>
			<td width="80">
			<table border="0" width="<?php if($total!=0) {echo 80*$qrow['ps']/$total."";} else {echo "0";}?>" cellspacing="0" cellpadding="0" height="5" bgcolor="<?php echo showColor($i);?>">
				<tr>
					<td></td>
				</tr>
			</table>
			</td>
			<td width="18%" nowrap>占<b><?php if($total!=0) {echo number_format($qrow['ps']/$total*100,2,".","")."%";} else {echo "0.00%";}?></b></td>
		</tr>
		<?php
		  }
		mysql_free_result($qry);
			?>
	</table><br>
	<?php
	  }
	mysql_free_result($query);	
	?>
</div>
<div align="center">
	<input class="btn" type="button" name="button" value="关闭" onClick="javascript:window.close()">
</div>

</body>

</html>
<?php mysql_close($conn);?>